<template>
  <div class="CommodityInformationManagement">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="款号/款名/套装编码" class="myform-item">
          <el-input
            v-model="form.title"
            placeholder="请输入款号/款名/套装编码"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="CommodityInformationManagement-select">
      <div class="CommodityInformationManagement-select-one">
        <div>选择标签</div>
        <div>
          已选标签
          <span>0</span>
        </div>
        <el-button type="text" style="color: red">重置</el-button>
      </div>
      <div class="CommodityInformationManagement-select-two">
        <div>
          <div>形制</div>
          <div>
            <el-checkbox
              v-for="city in options"
              :key="city.id"
              @change="onChangeXZ($event, city.id)"
              >{{ city.title }}</el-checkbox
            >
          </div>
        </div>
        <template v-for="(item, index) in optionsTag">
          <div :key="item.id" v-if="index < tagNum">
            <div>{{ item.title }}</div>
            <div>
              <el-checkbox
                v-for="city in item.list"
                :key="city.id"
                @change="onChangeTag($event, city.id)"
                >{{ city.title }}</el-checkbox
              >
            </div>
          </div>
        </template>
      </div>
    </div>
    <div>
      <el-button type="text" v-if="tagNum == 4" @click="tagNum = 1000">
        更多标签
        <i class="el-icon-d-arrow-right el-icon--right"></i>
      </el-button>
      <el-button type="text" v-else @click="tagNum = 4">
        收起
        <i class="el-icon-d-arrow-left el-icon--right"></i>
      </el-button>
    </div>
    <el-table :data="tableData" style="width: 100%" border size="mini">
      <el-table-column
        label="套装信息"
        align="center"
        prop="package_number"
        width="500"
      >
        <template slot-scope="{ row }">
          <div class="goodsDetail">
            <div class="goodsDetail-img">
              <el-image
                :src="row.image[0]"
                fit="contain"
                :preview-src-list="row.image"
              ></el-image>
            </div>
            <div class="goodsDetail-data">
              <div>
                <div>营</div>
                <div class="u-text-1">
                  {{ row.list[0].package_yunyin_name || "暂无运营套名" }}
                </div>
                <el-button
                  type="text"
                  @click="onYYDialogVisible(row, 1)"
                  v-if="qxobj.save"
                >
                  <i class="el-icon-edit-outline" style="font-size: 20px"></i>
                </el-button>
              </div>
              <div>
                <div>产</div>
                <div>{{ row.list[0].package_name || "暂无产品套名" }}</div>
              </div>
              <div>套装编码：{{ row.package_number }}</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="套装标签" align="center" prop="label" width="200">
        <template slot-scope="{ row }">
          <div>
            <el-tag
              v-if="row.fromdata"
              style="margin-right: 20px; margin-bottom: 10px"
              effect="dark"
              :color="row.fromdata.font_color"
              >{{ row.fromdata.title }}</el-tag
            >
            <el-tag
              style="margin-right: 20px; margin-bottom: 10px"
              v-for="tag in row.label"
              :key="tag.id"
              effect="dark"
              :color="tag.font_color"
              >{{ tag.title }}</el-tag
            >
          </div>
          <el-button
            type="text"
            @click="onEditTao(row.package_number)"
            v-if="qxobj.save"
          >
            <i class="el-icon-edit-outline" style="font-size: 20px"></i>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="商品信息" align="center" prop="image">
        <template slot-scope="{ row }">
          <el-table :data="row.list" style="width: 100%" border>
            <el-table-column
              label="款号"
              align="center"
              prop="type_no"
              width="120"
            >
            </el-table-column>
            <el-table-column
              label="商品款名/短名称"
              align="center"
              prop="type_name"
            >
              <template slot-scope="{ row }">
                <div class="abbreviation">
                  <div class="u-text-1">{{ row.type_name }}</div>
                  <div>/</div>
                  <div>{{ row.type_short_name || "暂无" }}</div>
                  <el-button
                    type="text"
                    @click="onYYDialogVisible(row, 2)"
                    v-if="qxobj.save"
                  >
                    <i class="el-icon-edit-outline" style="font-size: 20px"></i>
                  </el-button>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="品类" align="center" prop="type_name">
              <template slot-scope="{ row }">
                <div class="abbreviation">
                  <div class="u-text-1">{{ row.category_one_name }}</div>
                  <div>{{ row.category_two_name ? "/" : "" }}</div>
                  <div>{{ row.category_two_name }}</div>
                  <el-button
                    type="text"
                    @click="onYYDialogVisible(row, 3)"
                    v-if="qxobj.save"
                  >
                    <i class="el-icon-edit-outline" style="font-size: 20px"></i>
                  </el-button>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="{ row }">
                <div class="operation">
                  <el-button
                    type="text"
                    @click="onEdit(row.id)"
                    v-if="qxobj.save"
                    >编辑</el-button
                  >
                  <el-button
                    type="text"
                    @click="clickOms(row.type_no)"
                    v-if="qxobj.save"
                    >推送</el-button
                  >
                  
                  <el-image
                    :src="tableimage"
                    fit="fill"
                    class="pointer"
                    style="width: 20px"
                    @click="onTable(row.type_no)"
                  ></el-image>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminOperateGetProductslist"
    />
    <el-dialog
      title="运营套名"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      v-if="rowDetail.package_number"
    >
      <div class="dialogVisibleTZ">
        <div class="dialogVisibleTZ-one">
          <div>套装编码：{{ rowDetail.package_number }}</div>
          <div>
            产品套名：{{
              rowDetail && (rowDetail.list[0].package_name || "暂无产品套名")
            }}
          </div>
        </div>
        <div class="dialogVisibleTZ-two">
          <div>运营套名：</div>
          <el-input
            v-model="package_yunyin_name"
            placeholder="请输入运营套名"
            size="mini"
            clearable
          ></el-input>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button
          type="primary"
          :disabled="!package_yunyin_name"
          @click="onYYEdit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      title="短名称"
      :visible.sync="dialogVisibleDMC"
      width="30%"
      :before-close="handleClose"
    >
      <div class="dialogVisibleTZ">
        <div class="dialogVisibleTZ-one">
          <div>款号：{{ rowDetail.type_no }}</div>
          <div>
            商品款名：{{ rowDetail && (rowDetail.type_name || "暂无商品款名") }}
          </div>
        </div>
        <div class="dialogVisibleTZ-two">
          <div>短名称：</div>
          <el-input
            v-model="type_short_name"
            placeholder="请输入短名称"
            size="mini"
            clearable
          ></el-input>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button
          type="primary"
          :disabled="!type_short_name"
          @click="onYYEditDMC"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      title="品类"
      :visible.sync="dialogVisiblePL"
      width="30%"
      :before-close="handleClose"
    >
      <div class="dialogVisibleTZ">
        <div class="dialogVisibleTZ-one">
          <div>款号：{{ rowDetail.type_no || "" }}</div>
          <div>
            商品款名：{{ rowDetail && (rowDetail.type_name || "暂无商品款名") }}
          </div>
        </div>
        <div class="dialogVisibleTZ-two">
          <div>一级品类：</div>
          <el-select
            v-model="category_one"
            placeholder="请选择"
            @change="changeCategory"
          >
            <el-option
              v-for="item in optionsOne"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <div class="dialogVisibleTZ-two">
          <div>二级品类：</div>
          <el-select v-model="category_two" placeholder="请选择">
            <el-option
              v-for="item in optionsTwo"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button
          type="primary"
          :disabled="!category_one || !category_two"
          @click="onYYEditPL"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  postAdminOperateGetProductslist,
  postAdminOperateGetProductsLabellist,
  postAdminOperateSetpackage_number,
  postApigetcategory,
  postAdminOmsaddProduct
} from "@/api";
import pagination from "@/components/pagination.vue";
import tableimage from "@/assets/images/table.png";
export default {
  components: { pagination },
  data() {
    return {
      tableimage,
      dialogVisible: false,
      dialogVisibleDMC: false,
      dialogVisiblePL: false,
      qxobj: {},
      package_yunyin_name: "",
      type_short_name: "",
      category_one: "",
      category_two: "",
      rowDetail: "",
      form: {
        title: "",
        labelids: "",
        formid: "",
        page: 1,
        size: 10,
      },
      tagNum: 4,
      checkedCities: [],
      options: [],
      optionsOne: [], // 一级品类
      optionsTwo: [], // 二级品类
      optionsTag: [],
      tableData: [],
      total: 0,
      xzIDs: [],
      tagIDs: [],
    };
  },
  activated() {
    let { form } = this;
    this.qxobj = JSON.parse(this.$route.query.qxobj);
    this.postAdminOperateGetProductsLabellist();
    this.postAdminOperateGetProductslist(form);
    this.postApigetcategory();
  },
  methods: {
    async clickOms(e) {
      let obj = {
        type_no: e,
        action_type: "add",
      };
      // if(e.is_oms==1){
      //     obj.action_type = "update"
      // }else{
      //     obj.action_type = "add"
      // }
      try {
        let res = await postAdminOmsaddProduct(obj);
        this.$message.success("推送成功");
      } catch (error) {}
    },
    handleClose() {
      this.package_yunyin_name = "";
      this.type_short_name = "";
      this.category_one = "";
      this.category_two = "";
      this.row = "";
      this.dialogVisible = false;
      this.dialogVisibleDMC = false;
      this.dialogVisiblePL = false;
    },
    async onYYDialogVisible(e, type) {
      this.rowDetail = e;
      if (type == 1) {
        this.package_yunyin_name = e.list[0].package_yunyin_name;
        this.dialogVisible = true;
        return;
      }
      if (type == 2) {
        this.type_short_name = e.type_short_name || "";
        this.dialogVisibleDMC = true;
        return;
      }
      if (type == 3) {
        this.category_one = e.category_one || "";
        this.category_two = e.category_two || "";
        if (e.category_one) {
          let obj = {
            fid: e.category_one,
            page: 1,
            size: 10000,
          };
          let res = await postApigetcategory(obj);
          let data = res.data;
          data = data.filter((e) => {
            return e.status != 0;
          });
          this.optionsTwo = data;
        }
        this.dialogVisiblePL = true;
        return;
      }
    },
    async postApigetcategory() {
      let obj = {
        fid:0,
        page: 1,
        size: 10000,
      };
      let res = await postApigetcategory(obj);
      let data = res.data;
      data = data.filter((e) => {
        return e.status != 0;
      });
      this.optionsOne = data;
    },
    async changeCategory(e) {
      let obj = {
        fid: e,
        page: 1,
        size: 10000,
      };
      this.category_two = "";
      let res = await postApigetcategory(obj);
      let data = res.data;
      data = data.filter((e) => {
        return e.status != 0;
      });
      this.optionsTwo = data;
    },
    // 修改运营套名
    async onYYEdit() {
      let { rowDetail, package_yunyin_name, form } = this;
      let obj = {
        package_number: rowDetail.package_number,
        package_yunyin_name,
      };
      try {
        let res = await postAdminOperateSetpackage_number(obj);
        this.$message.success("成功");
        this.postAdminOperateGetProductslist(form);
        this.handleClose();
      } catch (error) {}
    },
    // 修改短名称
    async onYYEditDMC() {
      let { rowDetail, type_short_name, form } = this;
      let obj = {
        type_no: rowDetail.type_no,
        type_short_name,
      };
      try {
        let res = await postAdminOperateSetpackage_number(obj);
        this.$message.success("成功");
        this.postAdminOperateGetProductslist(form);
        this.handleClose();
      } catch (error) {}
    },
    // 修改品类
    async onYYEditPL() {
      let { rowDetail, category_one, category_two, form } = this;
      let obj = {
        type_no: rowDetail.type_no,
        category_one,
        category_two,
      };
      try {
        let res = await postAdminOperateSetpackage_number(obj);
        this.$message.success("成功");
        this.postAdminOperateGetProductslist(form);
        this.handleClose();
      } catch (error) {}
    },
    onTable(type_no) {
      this.$router.push({
        name: "OperationalDepartmentBasic",
        query: { type_no },
      });
    },
    // 形制选择
    onChangeXZ(e, id) {
      let { xzIDs } = this;
      console.log("形制选择---->", e, id);
      if (e) {
        xzIDs.push(id);
        this.xzIDs = xzIDs;
      } else {
        xzIDs.splice(xzIDs.indexOf(id), 1);
        this.xzIDs = xzIDs;
      }
      this.form.formid = xzIDs.toString();
      this.onSearch();
    },
    // 标签选择
    onChangeTag(e, id) {
      let { tagIDs } = this;
      console.log("形制选择---->", e, id);
      if (e) {
        tagIDs.push(id);
        this.tagIDs = tagIDs;
      } else {
        tagIDs.splice(tagIDs.indexOf(id), 1);
        this.tagIDs = tagIDs;
      }
      this.form.labelids = tagIDs.toString();
      this.onSearch();
    },
    // 标签列表
    postAdminOperateGetProductsLabellist() {
      postAdminOperateGetProductsLabellist().then((res) => {
        this.options = res.data.fromdata.list;
        this.optionsTag = res.data.label;
      });
    },
    // 查看
    onEdit(id) {
      this.$router.push({
        name: "OperationalDepartmentCommodityInformationManagementEdit",
        query: { id },
      });
    },
    // 查看(套)
    onEditTao(id) {
      this.$router.push({
        name: "OperationalDepartmentCommodityInformationManagementEditTao",
        query: { id },
      });
    },

    // 采购列表
    async postAdminOperateGetProductslist(data) {
      this.loading = true;
      this.form = data;
      postAdminOperateGetProductslist(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminOperateGetProductslist(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.dialogVisibleTZ {
  .dialogVisibleTZ-one {
    display: flex;
    > div:nth-child(1) {
      margin-right: 40px;
    }
  }
  .dialogVisibleTZ-two {
    margin-top: 20px;
    display: flex;
    align-items: center;
    > div:nth-child(1) {
      width: 100px;
    }
  }
}
.operation {
  display: flex;
  align-items: center;
  justify-content: center;
  > .el-image {
    margin-left: 20px;
  }
}
.abbreviation {
  display: flex;
  align-items: center;
  justify-content: center;
  > div:nth-child(1) {
    max-width: 180px;
  }
}
.goodsDetail {
  display: flex;
  align-items: center;
  justify-content: center;
  .goodsDetail-img {
    width: 100px;
    height: 100px;
  }
  .goodsDetail-data {
    width: 300px;
    margin-left: 10px;
    > div:nth-child(1) {
      display: flex;
      align-items: center;
      > div:nth-child(1) {
        color: #67c23a;
        font-size: 12px;
        text-align: center;
        line-height: 24px;
        width: 24px;
        height: 24px;
        margin-right: 5px;
        border: 1px solid #67c23a;
        border-radius: 50%;
      }
      > div:nth-child(2) {
        font-size: 14px;
        text-align: left;
        margin-right: 5px;
        max-width: 220px;
      }
    }
    > div:nth-child(2) {
      display: flex;
      align-items: center;
      > div:nth-child(1) {
        color: #2c70d6;
        font-size: 12px;
        text-align: center;
        line-height: 24px;
        width: 24px;
        height: 24px;
        margin-right: 5px;
        border: 1px solid #2c70d6;
        border-radius: 50%;
      }
      > div:nth-child(2) {
        text-align: left;
        font-size: 14px;
        margin-right: 5px;
        max-width: 240px;
      }
    }
    > div:nth-child(3) {
      margin-top: 15px;
      text-align: left;
    }
  }
}
.CommodityInformationManagement {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
  .CommodityInformationManagement-select {
    .CommodityInformationManagement-select-one {
      display: flex;
      align-items: center;
      > div:nth-child(1) {
        font-weight: 800;
        margin-right: 20px;
      }
      > div:nth-child(2) {
        margin-right: 20px;
        > span {
          color: #409eff;
        }
      }
    }
    .CommodityInformationManagement-select-two {
      > div {
        display: flex;
        flex-wrap: wrap;
        height: 40px;
        align-items: center;
        > div:nth-child(1) {
          width: 100px;
          font-size: 14px;
        }
        > div:nth-child(2) {
          width: calc(100% - 100px);
          display: flex;
          flex-wrap: wrap;
        }
      }
    }
  }
}
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
</style>